package com.sunday.pinxinghui.base;

/**
 * Created by 刘涛 on 2017/11/10.
 */

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.sunday.common.base.BaseActivity;
import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;

import static android.Manifest.permission.CALL_PHONE;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

public abstract class NeedPhonePermissionActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public void checkCallPermission() {
        RationaleListener rationaleListener = new RationaleListener() {
            @Override
            public void showRequestPermissionRationale(int requestCode, final Rationale rationale) {
                AlertDialog.newBuilder(mContext)
                        .setTitle("温馨提示")
                        .setMessage("请开启拨打电话权限")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                rationale.resume();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        rationale.cancel();
                                    }
                                }
                        ).show();
            }
        };
        AndPermission.with(this)
                .requestCode(100)
                .permission(CALL_PHONE, READ_PHONE_STATE)
                .rationale(rationaleListener)
                .callback(listener)
                .start();
    }


    private PermissionListener listener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
            switch (requestCode) {
                case 100:
                    if (AndPermission.hasPermission(mContext, WRITE_EXTERNAL_STORAGE, READ_PHONE_STATE)) {
                        onRequestCallPermissionSuccessed();
                    } else {
                        AndPermission.defaultSettingDialog(mContext)
                                .setTitle("权限申请失败")
                                .setMessage("为确保app正常使用，请在设置中授权")
                                .setPositiveButton("去设置")
                                .show();
                    }
                    break;
            }
        }

        @Override
        public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
            switch (requestCode) {
                case 100:
                    if (AndPermission.hasPermission(mContext, CALL_PHONE, READ_PHONE_STATE)) {
                        onRequestCallPermissionSuccessed();
                    } else {
                        AndPermission.defaultSettingDialog(mContext)
                                .setTitle("权限申请失败")
                                .setMessage("为确保app正常使用，请在设置中授权拨打电话")
                                .setPositiveButton("去设置")
                                .show();
                    }
                    break;

            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (AndPermission.hasPermission(mContext, CALL_PHONE, READ_PHONE_STATE)) {
            onRequestCallPermissionSuccessed();
        } else {
            AndPermission.defaultSettingDialog(mContext)
                    .setTitle("拨打电话权限申请失败")
                    .setMessage("为确保app正常使用，请在设置中授权")
                    .setPositiveButton("去设置")
                    .show();
        }

    }

    public abstract boolean onRequestCallPermissionSuccessed();


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
